package org.polyforms.repository.strategy.support;

import java.lang.reflect.Method;
import java.util.Set;

import javax.inject.Inject;
import javax.inject.Named;

import org.polyforms.repository.executor.ClassBasedExecutor;
import org.polyforms.repository.executor.Executor;
import org.polyforms.repository.strategy.ExecutorFindingStrategy;

/**
 * Strategy of finding {@link Executor} inheriting {@link ClassBasedExecutor} for specific method.
 * 
 * @author Kuisong Tong
 * @since 1.0
 */
@Named
public final class ClassBasedExecutorFindingStrategy implements ExecutorFindingStrategy {
    private final Set<ClassBasedExecutor> executors;

    /**
     * Create an instance with {@link ClassBasedExecutor}s.
     */
    @Inject
    public ClassBasedExecutorFindingStrategy(final Set<ClassBasedExecutor> executors) {
        this.executors = executors;
    }

    /**
     * {@inheritDoc}
     */
    public Executor getExecutor(final Method method) {
        for (final ClassBasedExecutor executor : executors) {
            if (executor.getMatchedInterface() == method.getDeclaringClass()) {
                return executor;
            }
        }
        return Executor.UNSUPPORT;
    }
}
